<?php
namespace plugins\order\model;
use think\Model;
use think\Db;

class OrderMemberModel extends Model
{

	//订单付款
	public function orderPayment($order_member_num,$money_type)
	{
		//订单状态的检测
		$order_member = ( new \plugins\order\model\OrderMemberModel )->get(['order_member_num'=>$order_member_num]);

		if($order_member == null){
			return api_json_echo(false,'订单号错误');
		}

		if($order_member->pay == 1){
			return api_json_echo(false,'订单已支付');
		}

		//店铺订单的数组
		$order_num_list = json_decode($order_member->order_num);

		foreach ($order_num_list as $key => $value) {
			//得到店铺订单详情
			$order = ( new \plugins\order\model\OrderModel )->get(['order_num'=>$value]);
			//更改支付方式
			if($money_type == "alipay"){
				$order->money_type = 2;
			}
			if($money_type == "wechat"){
				$order->money_type = 3;
			}
			if($money_type == "balance"){
				$order->money_type = 4;
			}
			//更改订单状态
			if($order->order_status > 2){
				$order->order_status = 2;
			}
			
			$order->save();
		}

		$order_member->pay = 1;
		$order_member->save();

	}

	//订单关闭
	public function orderMemberClose($order_member_num)
	{
		//订单状态的检测
		$order_member = ( new \plugins\order\model\OrderMemberModel )->get(['order_member_num'=>$order_member_num]);

		if($order_member == null){
			return false;
			// return api_json_echo(false,'订单号错误');
		}

		if($order_member->pay == 1){
			return false;
			// return api_json_echo(false,'订单已支付');
		}

		//店铺订单的数组
		$order_num_list = json_decode($order_member->order_num);

		foreach ($order_num_list as $key => $value) {
			//得到店铺订单详情
			$order = ( new \plugins\order\model\OrderModel )->get(['order_num'=>$value]);
			//更改订单状态为关闭
			$order->order_status = 5;

			$order->save();
		}
		//大订单改为关闭
		$order_member->pay = 3;
		$order_member->save();

	}

	//检查全局的
	public function overTimeOrderCheck()
	{
		return true;
		$where = (time()-1800)." >= time and pay = 0";
		$list = db('order_member')->field('order_member_num')->where($where)->select()->toArray();
		foreach ($list as $key => $value) {
			$this->orderMemberClose($value['order_member_num']);
		}
	}

}